package io.adrainty.bolee.trade.handler.alipay;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.common.models.AlipayTradeCloseResponse;
import com.alipay.easysdk.payment.wap.models.AlipayTradeWapPayResponse;
import io.adrainty.bolee.framework.commons.basic.ResponseCode;
import io.adrainty.bolee.framework.commons.exception.AppException;
import io.adrainty.bolee.trade.handler.AbstractPayHandler;
import io.adrainty.bolee.trade.models.constants.TradeConstants;
import io.adrainty.bolee.trade.models.entity.PayChannelEntity;
import io.adrainty.bolee.trade.models.entity.TradeEntity;
import io.adrainty.bolee.trade.models.valobj.PayChannelEnum;
import io.adrainty.bolee.trade.models.valobj.TradeStatusEnum;
import io.adrainty.bolee.trade.utils.AliPayUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * @author AdRainty
 * @version V1.0.0
 * @since 2025/8/31 下午11:33
 */

@Slf4j
@Component
public class AliPayHandler extends AbstractPayHandler {

    @Override
    protected TradeEntity doWapTrade(TradeEntity tradeVO, PayChannelEntity payChannel) {
        Config aliPayConfig = AliPayUtils.getAliPayConfig(payChannel);

        try {
            Factory.setOptions(aliPayConfig);
            AlipayTradeWapPayResponse response = Factory.Payment.Wap().pay(
                    tradeVO.getMemo(),
                    String.valueOf(tradeVO.getTradeOrderNo()),
                    String.valueOf(new BigDecimal("0.01")),
                    tradeVO.getQuitUrl(),
                    tradeVO.getReturnUrl());
            boolean isSuccess = ResponseChecker.success(response);
            if (isSuccess && StringUtils.isNotBlank(response.body)) {
                tradeVO.setPlaceOrderCode(TradeConstants.ALI_SUCCESS_CODE);
                tradeVO.setPlaceOrderMsg(TradeConstants.ALI_SUCCESS_MSG);
                tradeVO.setPlaceOrderJson(response.getBody());
                tradeVO.setTradeState(TradeStatusEnum.WAIT_PAY);

                tradeRepository.save(tradeVO);
                return tradeVO;
            } else {
                log.error("支付宝下单失败: {}", response.getBody());
                throw new AppException(ResponseCode.TRADE_ERROR);
            }
        } catch (Exception e) {
            if (e instanceof AppException ae) {
                throw ae;
            }
            log.error("支付宝下单异常: ", e);
            throw new AppException(ResponseCode.TRADE_ERROR);
        }
    }

    @Override
    protected void doCloseTrade(Long tradeOrderNo, PayChannelEntity payChannel) {
        try {
            Config aliPayConfig = AliPayUtils.getAliPayConfig(payChannel);
            Factory.setOptions(aliPayConfig);
            AlipayTradeCloseResponse response = Factory.Payment.Common()
                    .close(String.valueOf(tradeOrderNo));
            if (!ResponseChecker.success(response)) {
                log.error("支付宝关闭订单失败: {}", response.getHttpBody());
                throw new AppException(ResponseCode.CLOSE_TRADE_ERROR);
            }
        } catch (Exception e) {
            if (e instanceof AppException ae) {
                throw ae;
            }
            log.error("支付宝关闭订单异常: ", e);
            throw new AppException(ResponseCode.CLOSE_TRADE_ERROR);
        }
    }

    @Override
    public PayChannelEnum supportPayChannel() {
        return PayChannelEnum.ALI_PAY;
    }

}
